package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.po.Dept;
import com.itheima.domain.po.Emp;
import com.itheima.domain.query.EmpQueryParam;
import com.itheima.domain.vo.PageResult;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.time.LocalDate;
import java.util.List;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {
    @Autowired
    DeptMapper deptMapper;

    public PageResult<Emp> pageEmp1(EmpQueryParam param) {
        //1-参数校验：分页参数异常抛出异常
        Integer pageNum = param.getPage();
        Integer pageSize = param.getPageSize();
        if (ObjectUtils.isNull(pageNum) || ObjectUtils.isNull(pageSize)) {
            throw new RuntimeException("pageNum or pageSize is null");
        }
        String name = param.getName();
        Integer gender = param.getGender();
        LocalDate begin = param.getBegin();
        LocalDate end = param.getEnd();

        //2-分页查询：对emp条件分页查询List<Emp>-->循环遍历，根据dept_id查询dept表获取部门名称填充deptName
        //2.1 分页查询
        //等价SQL： select * from emp where name like “%？%” and user_sex=?  order by update_time desc limit ?,?
        Page<Emp> page = Page.of(pageNum, pageSize);
        LambdaQueryWrapper<Emp> lambdaQueryWrapper = Wrappers.<Emp>lambdaQuery()
                                                             .like(StringUtils.isNotBlank(name), Emp::getName, name)
                                                             .eq(gender != null, Emp::getGender, gender)
                                                             .between(begin != null && end != null && begin.isBefore(end), Emp::getEntryDate, begin, end)
                                                             .orderByDesc(Emp::getUpdateTime);
        this.page(page, lambdaQueryWrapper);

        //LambdaQueryChainWrapper=支持lambada+将条件+目标方法串起来
        this.lambdaQuery()
            .like(StringUtils.isNotBlank(name), Emp::getName, name)
            .eq(gender != null, Emp::getGender, gender)
            .between(begin != null && end != null && begin.isBefore(end), Emp::getEntryDate, begin, end)
            .orderByDesc(Emp::getUpdateTime).page(page);

        //2.2 循环填充部门名称
        List<Emp> empList = page.getRecords();
        if (CollectionUtils.isNotEmpty(empList)) {
            empList.forEach(emp -> {
                //根据deptId查询部门名称
                Dept dept = deptMapper.selectById(emp.getDeptId());
                Assert.notNull(dept, "员工关联的部门是不能为空的");
                emp.setDeptName(dept.getName());
            });
        }
        return PageResult.<Emp>builder().total(page.getTotal()).rows(empList).build();
    }

    @Override
    public PageResult<Emp> pageEmp(EmpQueryParam param) {
        //1-参数校验：分页参数异常抛出异常
        Integer pageNum = param.getPage();
        Integer pageSize = param.getPageSize();
        if (ObjectUtils.isNull(pageNum) || ObjectUtils.isNull(pageSize)) {
            throw new RuntimeException("pageNum or pageSize is null");
        }
        String name = param.getName();
        Integer gender = param.getGender();
        LocalDate begin = param.getBegin();
        LocalDate end = param.getEnd();
        //2-通过mapper调用方法一次性分页动态条件查询员工
        //复杂SQL分页: select emp.*,dept.name deptName from emp left join  dept on  emp.detp_id=dept.id where order by limit
        //单表SQL分页：select * from emp where xx limit
        Page<Emp> page = Page.of(pageNum, pageSize);
        this.getBaseMapper().selectEmpPage(page, param);
        return PageResult.<Emp>builder().total(page.getTotal()).rows(page.getRecords()).build();
    }
}